﻿using System;
using System.IO;

namespace Emirps
{
    class Program
    {
        static void Main(string[] args)
        {
            TextReader reader = Console.In;
            string allText = reader.ReadToEnd();

            string[] lines = allText.Split('\n');

            foreach (string line in lines)
            {
                Int64 limit = 0;
                Int64 result = 0;

                if (Int64.TryParse(line, out limit) == true)
                {
                    if (limit < 13)
                    {
                        Console.WriteLine("0");
                    }
                    for (Int64 i = 13; i < limit; i++)
                    {
                        if (IsEmirp(i))
                        {
                            result += i;
                        }
                    }
                }

                Console.WriteLine(result.ToString());
            }
        }

        private static bool IsEmirp(Int64 number)
        {
            char[] tmp = number.ToString().ToCharArray();
            Array.Reverse(tmp);

            Int64 emirp = 0;
            if (Int64.TryParse(new string(tmp), out emirp) == true)
            {
                return IsPrime(emirp) && IsPrime(number) && (emirp != number);
            }
            return false;
        }

        private static bool IsPrime(Int64 number)
        {
            double division = (double)number / 2.0;
            if ((division - Math.Truncate(division)) == 0)
            {
                return false;
            }
            double sqrt = Math.Sqrt(number);
            if ((sqrt - Math.Truncate(sqrt)) == 0)
            {
                return false;
            }
            for (Int64 i = 2; i <= (Int64)sqrt; i++)
            {
                if (number % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
